import { contextBridge, ipcRenderer } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'

// Custom APIs for renderer
const api = {
  // 向渲染进程发送消息
  getPath: (callback) => ipcRenderer.on('folder-path-selected', callback),
  // 选择文件
  getFilePath: (callback) => ipcRenderer.on('file-path-selected', callback),
  //移除监听
  removeGetPath: () => ipcRenderer.removeAllListeners('folder-path-selected'),
  // 移除监听
  removeGetFilePath: () => ipcRenderer.removeAllListeners('file-path-selected'),
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  window.electron = electronAPI
  window.api = api
}
